from .models import GoodsChannel

def get_breadcrumb(category):
    """
    获取面包屑导航
    :param category: 类别对象: 一级, 二级, 三级
    :return: 面包屑导航字典
             一级: 返回一级;
             二级: 返回一级+二级;
             三级: 返回一级+二级+三级
    """
    breadcrumb = dict(
        cat1='',
        cat2='',
        cat3=''
    )
    # 第一级没有父级
    if category.parent is None:
        breadcrumb['cat1'] = category
    # 第三级没有子集
    elif not category.subs.exists():
        breadcrumb['cat3'] = category
        breadcrumb['cat2'] = category.parent
        breadcrumb['cat1'] = category.parent.parent
    else:
        breadcrumb['cat2'] = category
        breadcrumb['cat1'] = category.parent
    # # 一类别的url在GoodsChannel表里
    url = GoodsChannel.objects.get(category_id=breadcrumb['cat1'].id).url
    setattr(breadcrumb['cat1'], 'url', url)
    return breadcrumb